package org.csu.onlineedubackend.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> implements Serializable {

    private int status;
    private String message;
    private T data;

    private CommonResponse(int status, String message) {
        this.status = status;
        this.message = message;
    }

    private CommonResponse(int status, String message, T data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return this.status == ResponseCode.SUCCESS.getCode();
    }

    public static <T> CommonResponse<T> createForSuccess() {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc());
    }

    public static <T> CommonResponse<T> createForSuccess(T data) {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), ResponseCode.SUCCESS.getDesc(), data);
    }

    public static <T> CommonResponse<T> createForSuccess(String message, T data) {
        return new CommonResponse<>(ResponseCode.SUCCESS.getCode(), message, data);
    }

    public static <T> CommonResponse<T> createForSuccess(int code, String message) {
        return new CommonResponse<>(code, message);
    }

    public static <T> CommonResponse<T> createForSuccess(int code, String message, T data) {
        return new CommonResponse<>(code, message, data);
    }

    public static <T> CommonResponse<T> createForError() {
        return new CommonResponse<>(ResponseCode.ERROR.getCode(), ResponseCode.ERROR.getDesc());
    }

    public static <T> CommonResponse<T> createForError(String errorMessage) {
        return new CommonResponse<>(ResponseCode.ERROR.getCode(), errorMessage);
    }

    public static <T> CommonResponse<T> createForError(int errorCode, String errorMessage) {
        return new CommonResponse<>(errorCode, errorMessage);
    }
    
    // 添加新的便捷方法
    public static <T> CommonResponse<T> success() {
        return createForSuccess();
    }
    
    public static <T> CommonResponse<T> success(T data) {
        return createForSuccess(data);
    }
    
    public static <T> CommonResponse<T> success(String message, T data) {
        return createForSuccess(message, data);
    }
    
    public static <T> CommonResponse<T> error(String errorMessage) {
        return createForError(errorMessage);
    }
    
    public static <T> CommonResponse<T> error(int errorCode, String errorMessage) {
        return createForError(errorCode, errorMessage);
    }
}
